import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('开始创建种子数据...');

  // 创建短剧分类
  const categories = await Promise.all([
    prisma.shortsCategory.upsert({
      where: { id: 1 },
      update: {},
      create: { id: 1, name: '都市情感' },
    }),
    prisma.shortsCategory.upsert({
      where: { id: 2 },
      update: {},
      create: { id: 2, name: '古装宫廷' },
    }),
    prisma.shortsCategory.upsert({
      where: { id: 3 },
      update: {},
      create: { id: 3, name: '悬疑推理' },
    }),
    prisma.shortsCategory.upsert({
      where: { id: 4 },
      update: {},
      create: { id: 4, name: '青春校园' },
    }),
  ]);

  console.log('短剧分类创建完成:', categories.map(c => c.name));

  // 创建短剧方向
  const directions = await Promise.all([
    prisma.shortsDirection.upsert({
      where: { id: 1 },
      update: {},
      create: { id: 1, name: '爱情' },
    }),
    prisma.shortsDirection.upsert({
      where: { id: 2 },
      update: {},
      create: { id: 2, name: '喜剧' },
    }),
    prisma.shortsDirection.upsert({
      where: { id: 3 },
      update: {},
      create: { id: 3, name: '悬疑' },
    }),
    prisma.shortsDirection.upsert({
      where: { id: 4 },
      update: {},
      create: { id: 4, name: '励志' },
    }),
  ]);

  console.log('短剧方向创建完成:', directions.map(d => d.name));

  // 创建测试用户
  const user = await prisma.user.upsert({
    where: { phone: '13800138000' },
    update: {},
    create: {
      phone: '13800138000',
      nickname: '测试用户',
      password: 'hashedpassword',
      loginType: 'PASSWORD',
      phoneVerified: true,
    },
  });

  console.log('测试用户创建完成:', user.nickname);

  // 创建短剧
  const short = await prisma.short.create({
    data: {
      title: '都市爱情故事',
      coverUrl: 'https://example.com/cover1.jpg',
      summary: '一个关于都市男女的爱情故事',
      description: '这是一个发生在现代都市的爱情故事，讲述了男女主角从相识到相爱的过程...',
      instructor: '张导演',
      viewCount: 1000,
      likeCount: 150,
      favoriteCount: 80,
      isTop: true,
      isFree: false,
      status: 'ONGOING',
      episodeCount: 20,
      totalDuration: 400,
      tags: ['爱情', '都市', '现代'],
      categoryId: 1, // 都市情感
      directionId: 1, // 爱情
      uploaderId: user.id,
    },
  });

  console.log('短剧创建完成:', short.title);

  // 创建短剧章节
  const chapters = await Promise.all([
    prisma.shortsChapter.create({
      data: {
        title: '第一章：初遇',
        description: '男女主角的第一次相遇',
        videoUrl: 'https://example.com/video1.mp4',
        coverUrl: 'https://example.com/chapter1.jpg',
        duration: 20,
        points: 0, // 第一章免费
        viewCount: 800,
        sort: 1,
        shortsId: short.id,
        uploaderId: user.id,
      },
    }),
    prisma.shortsChapter.create({
      data: {
        title: '第二章：相识',
        description: '两人开始了解彼此',
        videoUrl: 'https://example.com/video2.mp4',
        coverUrl: 'https://example.com/chapter2.jpg',
        duration: 18,
        points: 10, // 需要10积分
        viewCount: 600,
        sort: 2,
        shortsId: short.id,
        uploaderId: user.id,
      },
    }),
    prisma.shortsChapter.create({
      data: {
        title: '第三章：心动',
        description: '感情开始萌芽',
        videoUrl: 'https://example.com/video3.mp4',
        coverUrl: 'https://example.com/chapter3.jpg',
        duration: 22,
        points: 15, // 需要15积分
        viewCount: 450,
        sort: 3,
        shortsId: short.id,
        uploaderId: user.id,
      },
    }),
  ]);

  console.log('短剧章节创建完成:', chapters.map(c => c.title));

  // 创建第二个短剧
  const short2 = await prisma.short.create({
    data: {
      title: '古装宫廷传奇',
      coverUrl: 'https://example.com/cover2.jpg',
      summary: '一个关于宫廷权谋的故事',
      description: '这是一个发生在古代宫廷的故事，讲述了主角如何在复杂的宫廷环境中生存...',
      instructor: '李导演',
      viewCount: 800,
      likeCount: 120,
      favoriteCount: 60,
      isTop: false,
      isFree: true, // 免费短剧
      status: 'COMPLETED',
      episodeCount: 15,
      totalDuration: 300,
      tags: ['古装', '宫廷', '权谋'],
      categoryId: 2, // 古装宫廷
      directionId: 3, // 悬疑
      uploaderId: user.id,
    },
  });

  console.log('第二个短剧创建完成:', short2.title);

  // 创建第二个短剧的章节
  const chapters2 = await Promise.all([
    prisma.shortsChapter.create({
      data: {
        title: '第一章：入宫',
        description: '主角初入宫廷',
        videoUrl: 'https://example.com/video4.mp4',
        coverUrl: 'https://example.com/chapter4.jpg',
        duration: 25,
        points: 0, // 免费
        viewCount: 700,
        sort: 1,
        shortsId: short2.id,
        uploaderId: user.id,
      },
    }),
    prisma.shortsChapter.create({
      data: {
        title: '第二章：权谋',
        description: '宫廷中的明争暗斗',
        videoUrl: 'https://example.com/video5.mp4',
        coverUrl: 'https://example.com/chapter5.jpg',
        duration: 28,
        points: 0, // 免费
        viewCount: 550,
        sort: 2,
        shortsId: short2.id,
        uploaderId: user.id,
      },
    }),
  ]);

  console.log('第二个短剧章节创建完成:', chapters2.map(c => c.title));

  console.log('所有种子数据创建完成！');
}

main()
  .catch((e) => {
    console.error('种子数据创建失败:', e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  }); 